package org.laobai.permission.controller;


import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import org.laobai.permission.service.MenuService;
import org.laobai.permission.entity.Menu;
import org.laobai.permission.struct.PageResult;
import org.laobai.permission.struct.Result;
import org.laobai.permission.vo.SearchVO;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jiusan
 * @since 2021-11-17
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Resource
    private MenuService menuService;

    @GetMapping("getTree")
    public Result<List<Menu>> getTree() {
        /**
         * 根据用户ID 查询 用户角色
         * 根据用户角色ID 查询菜单
         */
        return Result.ok(menuService.getTree());
    }

    @GetMapping("updateStatus/{menuId}")
    public Result updateStatus(@PathVariable(name="menuId") Integer menuId) {
        return menuService.updateStatus(menuId) ? Result.ok() : Result.error();
    }

    @GetMapping("/{id}")
    public Result<Menu> getById(@PathVariable("id") Long id) {
        return Result.ok(menuService.getById(id));
    }

    @GetMapping
    public Result<List<Menu>> list() {
        return Result.ok(menuService.list());
    }

    @PostMapping
    public Result add(@RequestBody Menu menu) {
        return menuService.saveMenu(menu) ? Result.ok() : Result.error();
    }

    @PutMapping
    public Result update(@RequestBody Menu menu) {
        return menuService.updateMenu(menu) ? Result.ok() : Result.error();
    }

    @DeleteMapping("/{id}")
    public Result delById(@PathVariable("id") Long id) {
        return menuService.delById(id) ? Result.ok() : Result.error();
    }

    @PostMapping("search/{currPage}/{pageSize}")
    public PageResult<Menu> page(@PathVariable("currPage") Integer currPage,
                                 @PathVariable("pageSize") Integer pageSize,
                                 @RequestBody(required = false) SearchVO vo) {
        return menuService.page(currPage, pageSize, vo);
    }

    @PostMapping("delByIds")
    public Result delByIds(@RequestBody List<Long> ids) {
        return menuService.delByIds(ids) ? Result.ok() : Result.error();
    }

}
